package uk.ac.aber.Blockmation.Actions;

import uk.ac.aber.Blockmation.Viewer;

/**
 * Thread to Play the through the slides of a MovieModel and update the Display
 * of the Viewer
 * @author alex
 */
public class PlayMovieThread extends Thread {
    
    private Viewer viewer;
    int speed;

    
    public PlayMovieThread (Viewer viewer) {
        this.viewer = viewer;
    }

    @Override
    public void run() throws UnsupportedOperationException {
    	int index = 0;
            
        if (viewer.getLoop() == false) {

            while (index <= viewer.getMovieModel().size()-1) {
                try {

                    Thread.sleep(viewer.getSpeed());
                    viewer.getDisplay().setImage(viewer.getMovieModel().get(index));
                    index++;

                    /* commented out for use of seperating the viewer index from the movieModel
                    movieModel.setIndex(index);
                     */
                } catch (InterruptedException ex) {
                    //System.err.println("Thread Error");
                    return;
                }

                

            }

        } else {
            while (viewer.getLoop() == true) {
                index = 0;

                while (index <= viewer.getMovieModel().size()-1) {
                    
                    try {

                        Thread.sleep(viewer.getSpeed());
                        viewer.getDisplay().setImage( viewer.getMovieModel().get(index) );
                        index++;

                        /* commented out for use of seperating the viewer index from the movieModel
                        movieModel.setIndex(index);
                         */
                    
                    } catch (InterruptedException ex) {
                        System.err.println("Thread Error");
                    }
                }
            }
        }

    } 
    
}
